package qdu.com.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hdfs.FileUtil;
import qdu.com.bean.WeatherBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet("/bs")
public class BikeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        Map<Integer,String[]> map = FileUtil.ReadPartFile(getServletContext().getRealPath("/output/output")+"/part-r-00000");

        List<WeatherBean> list = new ArrayList<>();
        Iterator it = map.values().iterator();//使用现成的Iterator ：map.values().iterator
        while (it.hasNext()) {
            String[] w= (String[]) it.next();
            list.add(new WeatherBean(w[0],w[1]));
        }
        String json = mapper.writeValueAsString(list);
        System.out.println(json);
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write(json);
    }
//    private String testEcharts() throws IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        Option option = new Option();
//        Map<Integer, String[]> map = FileUtil.ReadPartFile("output/output/part-r-00000");
//
//        List<WeatherBean> list = new ArrayList<WeatherBean>();
//        Iterator it = map.values().iterator();//使用现成的Iterator ：map.values().iterator
//        while (it.hasNext()) {
//            String[] w= (String[]) it.next();
//            list.add(new WeatherBean(w[0],w[1]));
//        }
//        Gson gson2=new Gson();
//        String json=gson2.toJson(list);
//
//
//        return mapper.writeValueAsString(option);
//    }
}
